home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 940 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. Path: fido.asd.sgi.com!austern
  2. From: "Nathan Myers, http://www.cantrip.org/" <ncm@cantrip.org>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: string conversion to char*
  5. Date: 02 Apr 1996 11:11:56 PST
  6. Organization: The Cantrip Cooperative
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <3160CD2C.35CECA64@cantrip.org>
  9. References: <315A8E72.1D3C@master.grad.hr> <4jnm7h$134a@mule2.mindspring.com>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Mon, 01 Apr 1996 22:46:04 -0800
  12. X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i386)
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMWF7/Uy4NqrwXLNJAQEo8gIAlCzdjNicYQB7316GYMP1L3x70Aw3GsF9
  15.     eidc0dFM/ntWIQh+sNjnfhuDzLKxH26SO0Qtr0mQ28koE+v3Uo67ew==
  16.     =tqa9
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. Andrew Bell wrote:
  20. > Kresimir Fresl <fresl@master.grad.hr> wrote:
  21. > >According to proposed standard one should write:
  22. > >    ifstream (file_name.c_str());
  23. > >because implicit conversion of string to const char* is not defined.
  24. > >
  25. > >I agree that implicit conversion is (almost always) dangerous, and I
  26. > >_do not_ propose to add conversion operator to string class.
  27.  
  28. > Is there an explanation for why implicit conversion (at least to char
  29. > *) is dangerous?  It doesn't seem to me that requiring the member
  30. > function call is that much safer, although admittedly it is easier to
  31. > grep for.  However, the added complexity of code w/o a conversion
  32. > operator seems like it would add almost as many problems.
  33.  
  34. Member c_str() is safer because the person who calls it knows it 
  35. is being called.  The problem with implicit conversions is less that
  36. they are hard to grep for, but rather that they are often invoked 
  37. accidentally and invisibly.
  38.  
  39. If an implicit conversion were provided, the pointer would inevitably
  40. be passed to contexts in which its limited lifetime would not be kept
  41. track of, resulting in unreliable programs.  It would be nearly impossible
  42. to find these bugs, because the cause of the bug literally does not appear
  43. in the code.
  44.  
  45. The extra text ".cstr()" does not add to complexity; the complexity is
  46. present regardless.  It only makes the complexity visible.
  47.  
  48. Nathan Myers
  49. ncm@cantrip.org  http://www.cantrip.org/
  50. ---
  51. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  52.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  53.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  54.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  55.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  56. ]
  57.